use std::{env, fs, process};
use std::error::Error;

use crate::learn::MiniGrep::lib::{Config, search, search_case_insensitive};

fn run(config: Config) -> Result<(), Box<dyn Error>> {
    let contents = fs::read_to_string(config.fileName)?;
    let results = if config.case_sensitive {
        search(&config.query, &contents)
    } else {
        search_case_insensitive(&config.query, &contents)
    };
    for line in results {
        println!("{}", line);
    };
    Ok(())
}

pub fn main() {
    let config = Config::new(env::args()).unwrap_or_else(|err| {
        eprintln!("参数解析错误: {}", err);
        process::exit(1);
    });
    if let Err(e) = run(config) {
        eprintln!("程序错误: {}", e);
    }
}
